package com.ksyun.alert.task;

import com.ksyun.alert.calculate.CalculateTiming;
import com.ksyun.alert.config.CalculationTimingProperties;
import com.ksyun.alert.customException.SkyAlertException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 定时任务类
 * @author Zhang
 * @date 2024-04-30
 */
@Component
@Slf4j
public class TimingCalculationTask {
    @Resource
    private CalculateTiming calculateTiming;

    @Resource
    private CalculationTimingProperties calculationTimingProperties;

    @Scheduled(cron = "0/30 * * * * ?")
    public void processTimeOutOrder() {
        try {
            calculateTiming.calculateTiming(calculationTimingProperties.getStartTimestamp());
        } catch (Exception e) {
            throw new SkyAlertException(e.getMessage());
        }
    }

    @Scheduled(cron = "0/30 * * * * ?")
    public void processTimestamp() {
        try {
            calculationTimingProperties.setStartTimestamp(calculationTimingProperties.getStartTimestamp() + 30);
        } catch (Exception e) {
            throw new SkyAlertException(e.getMessage());
        }
    }
}
